package com.example.mongodb.service.impl;

import com.example.mongodb.entity.RestaurantInfo;
import com.example.mongodb.mongodb.DataQueueService;
import com.example.mongodb.repository.RestaurantInfoRepository;
import com.example.mongodb.service.RestaurantService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author : zhayh
 * @date : 2021-5-17 08:43
 * @description :
 */

@Service
public class RestaurantServiceImpl implements RestaurantService {
    @Resource
    private RestaurantInfoRepository restaurantInfoRepository;
    @Resource
    private DataQueueService dataQueueService;

    @Override
    public RestaurantInfo addRestaurant(RestaurantInfo restaurantInfo) {
        RestaurantInfo result = restaurantInfoRepository.save(restaurantInfo);
        if (result.getId() > 0) {
            dataQueueService.sendCreateMessage(result.getId());
        }
        return result;
    }

    @Override
    public RestaurantInfo updateRestaurant(RestaurantInfo restaurantInfo) {
        RestaurantInfo result = restaurantInfoRepository.save(restaurantInfo);
        if (result.getId() > 0) {
            dataQueueService.sendUpdateMessage(result.getId());
        }
        return result;
    }

    @Override
    public Boolean deleteRestaurant(Long id) {
        restaurantInfoRepository.deleteById(id);
        dataQueueService.sendDeleteMessage(id);
        return true;
    }
}
